<?php

namespace App\Http\Controllers;



use App\Models\Post;
use App\Models\Topic;

class TopicController extends Controller
{
    public function show(Topic $topic){
        $posts = $topic->posts()->with(['user'])->get();
        $my_posts = Post::authorBy()->topicNotIn($topic->id)->get();
        $topic->posts_count = count($posts);
        $topic->posts = $posts;
        return view('topic.show',compact('topic','my_posts'));
    }

    public function submit(Topic $topic){
        $post_ids = request('post_ids');
        if(!$post_ids){
            return err('','请勾选');
        }
        $topic->posts()->attach($post_ids);
        return res('','投稿成功');
    }



}
